/*
 * Copyright 2023-2024, Haiku, Inc. All rights reserved.
 * Distributed under the terms of the MIT License.
 *
 * 龙芯3A6000 EXTIOI中断控制器驱动
 * 参考Linux的drivers/irqchip/irq-loongson-extioi.c实现
 */

#ifndef _LOONGSON_EXTIOI_H
#define _LOONGSON_EXTIOI_H

#include <KernelExport.h>
#include <SupportDefs.h>

// EXTIOI寄存器定义
#define LOONGSON_EXTIOI_NODEMAP_BASE 0x0000
#define LOONGSON_EXTIOI_IPMAP_BASE 0x0080
#define LOONGSON_EXTIOI_ENABLE_BASE 0x0100
#define LOONGSON_EXTIOI_BOUNCE_BASE 0x0180
#define LOONGSON_EXTIOI_STATUS_BASE 0x0200

// EXTIOI配置常量
#define LOONGSON_EXTIOI_MAX_IRQS 256
#define LOONGSON_EXTIOI_MAX_NODES 4
#define LOONGSON_EXTIOI_IRQ_PER_REG 32

// 中断优先级定义
#define LOONGSON_EXTIOI_IP_0 0
#define LOONGSON_EXTIOI_IP_1 1
#define LOONGSON_EXTIOI_IP_2 2
#define LOONGSON_EXTIOI_IP_3 3
#define LOONGSON_EXTIOI_IP_4 4
#define LOONGSON_EXTIOI_IP_5 5
#define LOONGSON_EXTIOI_IP_6 6
#define LOONGSON_EXTIOI_IP_7 7

// 中断控制器结构体
typedef struct
{
    addr_t base_addr;                                 // 寄存器基地址
    uint32 irq_base;                                  // 中断基础编号
    uint32 num_irqs;                                  // 中断数量
    uint32 num_nodes;                                 // 节点数量
    uint32 node_map[LOONGSON_EXTIOI_MAX_IRQS / 32];   // 中断到节点映射
    uint32 ip_map[LOONGSON_EXTIOI_MAX_IRQS / 32];     // 中断到优先级映射
    uint32 irq_enable[LOONGSON_EXTIOI_MAX_IRQS / 32]; // 中断使能状态
} loongson_extioi;

// 函数声明
status_t loongson_extioi_init(addr_t base_addr, uint32 irq_base);
void loongson_extioi_uninit(void);

status_t loongson_extioi_enable_interrupt(uint32 irq);
status_t loongson_extioi_disable_interrupt(uint32 irq);
status_t loongson_extioi_configure_interrupt(uint32 irq, uint32 node, uint32 ip);

// 中断处理函数
int32 loongson_extioi_handle_interrupt(void *data);

// 定时器相关函数
status_t loongson_extioi_timer_init(void);
void loongson_extioi_timer_set_periodic(bigtime_t period);
void loongson_extioi_timer_set_event(bigtime_t timeout);
void loongson_extioi_timer_clear_event(void);
bigtime_t loongson_extioi_timer_get_time(void);

#endif // _LOONGSON_EXTIOI_H